﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace PRE
{
    public partial class frmnhanvien : PRE.Catalog.FrmBase
    {
        DAL.EntitiesDAL.nhanvien_DAL _dal = new DAL.EntitiesDAL.nhanvien_DAL();
        BLL.EntitiesBLL.nhanvienBLL _bll = new BLL.EntitiesBLL.nhanvienBLL();
        public frmnhanvien()
        {
            InitializeComponent();
        }
        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
            //DataTable _ma = _dal.Select_max();
            //if (_ma != null)
            //    if (_ma.Rows.Count > 0)
            //    {
            //        string ma = _bll.AddID(_ma.Rows[0]["id"] + "");
            //        txtmamon.Text = ma;
            ReadOnlyControl(false);
            ClearDataBindings();
            ChangeStatus(false);
            txtmanhanvien.ResetText();
            txthotennhanvien.ResetText();
            txtdiachi.ResetText();
            txtsodienthoai.ResetText();
            txtngayvaolam.ResetText();
            txtsongaylam.ResetText();
            txtluong.ResetText();
            txtgioitinh.ResetText();
            txtngaysinh.ResetText();
            txtcalam.ResetText();
            txtmanhanvien.Focus();
            //}


            base.PerformAdd();
        }
        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }
        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            string id = txtmanhanvien.Text;

            DialogResult result;
            var ok = _dal.Delete(id);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            ChangeStatus(false);
            txthotennhanvien.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            var name = Proper(txthotennhanvien.Text);


            if (IsAdd)
            {
                var o = new DAL.Entities.nhanvien()
                {
                    MANV  = txtmanhanvien.Text,
                    HOTEN = name,
                    DIACHI = txtdiachi.Text,
                    NGAYSINH = txtngaysinh.EditValue + "",
                    NGAYVAOLAM = txtngayvaolam.EditValue  + "",
                    DIENTHOAI =txtsodienthoai.Text ,
                    SONGAYLAM =txtsongaylam.Text,
                    LUONG =txtluong.Text ,
                    PHAI =txtgioitinh.Text,
                    CA=txtcalam.Text,


                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }

            }

            if (IsEdit)
            {
                //var _mabn = grvMain.GetFocusedRowCellValue("MaBN") + "";
                //string _madichvu = Text;
                var o = new DAL.Entities.nhanvien()
                {
                    MANV  = txtmanhanvien.Text,
                    HOTEN = name,
                    DIACHI = txtdiachi.Text,
                    NGAYSINH = txtngaysinh.EditValue + "",
                    NGAYVAOLAM = txtngayvaolam.EditValue  + "",
                    DIENTHOAI =txtsodienthoai.Text ,
                    SONGAYLAM =txtsongaylam.Text,
                    LUONG =txtluong.Text ,
                    PHAI =txtgioitinh.Text,
                    CA=txtcalam.Text,

                };

                var oki = _dal.Update(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            txthotennhanvien.Text = null;
            txtmanhanvien.Text = null;
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtmanhanvien.DataBindings.Clear();
            txthotennhanvien.DataBindings.Clear();
            txtdiachi.DataBindings.Clear();
            txtngayvaolam.DataBindings.Clear();
            txtsongaylam.DataBindings.Clear();
            txtluong.DataBindings.Clear();
            txtgioitinh.DataBindings.Clear();
            txtngaysinh.DataBindings.Clear();
            txtsodienthoai.DataBindings.Clear();
            txtcalam.DataBindings.Clear();
            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            txtmanhanvien.DataBindings.Add("EditValue", _dtb, ".MANV");
            txthotennhanvien.DataBindings.Add("EditValue", _dtb, ".HOTEN");
            txtdiachi.DataBindings.Add("EditValue", _dtb, ".DIACHI");
            txtngayvaolam.DataBindings.Add("EditValue", _dtb, ".NGAYVAOLAM");
            txtsongaylam.DataBindings.Add("EditValue", _dtb, ".SONGAYLAM");
            txtluong.DataBindings.Add("EditValue", _dtb, ".LUONG");
            txtgioitinh.DataBindings.Add("EditValue", _dtb, ".PHAI");
            txtngaysinh.DataBindings.Add("EditValue", _dtb, ".NGAYSINH");
            txtsodienthoai.DataBindings.Add("EditValue", _dtb, ".DIENTHOAI");
            txtcalam.DataBindings.Add("EditValue", _dtb, ".CA");
            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            txtmanhanvien.Properties.ReadOnly = isReadOnly;
            txthotennhanvien.Properties.ReadOnly = isReadOnly;
            txtdiachi.Properties.ReadOnly = isReadOnly;
            txtngayvaolam.Properties.ReadOnly = isReadOnly;
            txtsongaylam.Properties.ReadOnly = isReadOnly;
            txtluong.Properties.ReadOnly = isReadOnly;
            txtgioitinh.Properties.ReadOnly = isReadOnly;
            txtngaysinh.Properties.ReadOnly = isReadOnly;
            txtsodienthoai.Properties.ReadOnly = isReadOnly;
            txtcalam.Properties.ReadOnly = isReadOnly;
            DSnhanvien.Enabled = isReadOnly;
            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            base.PerformSearch();
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            _dtb = _dal.Select();


            if (_dtb != null)
            {
                DSnhanvien.DataSource = _dtb;
                string _dem1 = _dal.count_();
                thongke.Text = "Tổng số có  " + _dem1.ToString() + " nhân viên";
            }
            base.LoadData();
        }
        #endregion
        


        private void frmnhanvien_Load(object sender, EventArgs e)
        {

        }
    }
}
